import _config from './config';
import {
	dataToHump
} from './propertyNamingStrategy';

let _fetch = {};
let _header = {
	'session_id': wx.getStorageSync('sessionId') ? wx.getStorageSync('sessionId') : '0',
};
let _request = function (url, requsetHandler = {}) {
	let _data = requsetHandler.data;
	let _method = requsetHandler.method;
	let _headers = Object.assign({}, requsetHandler.header, _header);
	url = _config.host + url;

	wx.showLoading({
		title: '加载中',
	});

	return new Promise((resolve, reject) => {
		//debug
		// resolve({});
		// /*
		wx.request({
			url: url,
			data: _data,
			method: _method,
			header: _headers,
			success: function (response) {
				wx.hideLoading();
				if (response.statusCode === 200) {
					resolve(dataToHump(response.data));
				} else if (response.statusCode === 401) {
					let pages = getCurrentPages();
					if (pages[pages.length - 1].route.indexOf('mine/index') > -1) {
						reject('loginError');
					} else {
						wx.navigateTo({
							url: '../login/index',
						});
					}
				} else if (response.statusCode === 404 || response.statusCode === 500) {
					reject(response.data);
					wx.navigateTo({
						url: '../defaultPage/index?type=pageError'
					})
				} else if (response.statusCode === 408 || response.statusCode === 502 || response.statusCode === 503 || response.statusCode === 504) {
					reject(response.data);
					wx.navigateTo({
						url: '../defaultPage/index?type=noNetwork'
					})
				} else if (response.statusCode === 400) {
					reject(response.data);
				} else {
					reject('requestError');
				}
			},
			fail: function () {
				wx.hideLoading();
				wx.getNetworkType({
					success: function (res) {
						if (res.networkType == 'none') {
							reject('networkError');
						} else {
							reject('requestError');
						}
					}
				})
				// wx.showToast({
				// 	title: '网络请求失败',
				// 	icon: 'none',
				// });
				//登录失效
				// wx.removeStorage({
				// 	key: 'hasLogin',
				// 	success: function(res) {},
				// });
				// wx.removeStorage({
				// 	key: 'userInfo',
				// 	success: function(res) {
				// 		wx.navigateTo({
				// 			url: '/pages/login/login',
				// 		});
				// 	},
				// });
			},
			complete: function () {},
		});
		// */
	});
};

let _param = function (data) {
	let params = [];
	for (let i in data) {
		if (data[i] instanceof Array) {
			data[i].forEach(item => {
				params.push(i + '=' + encodeURIComponent(item));
			});
		} else {
			params.push(i + '=' + encodeURIComponent(data[i]));
		}
	}
	return params.join('&');
};

_fetch.setHeader = function (key, value) {
	_header[key] = value;
};

_fetch.get = function (url, data = {}) {
	let params = _param(data);
	if (params) {
		url += url.indexOf('?') > -1 && params.length > 0 ? '&' + params : '?' + params;
	}
	return _request(url, {
		method: 'GET',
		header: {
			'content-type': 'application/json'
		},
		data: '',
	});
};

_fetch.post = function (url, data = {}, header = {
	'content-type': 'application/json'
}) {
	return _request(url, {
		method: 'POST',
		header: header,
		data: data,
	});
};

_fetch.put = function (url, data = {}) {
	return _request(url, {
		method: 'PUT',
		header: {
			'content-type': 'application/json'
		},
		data: data,
	});
};

module.exports = _fetch;